package com.ylm.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylm.baseservice.handler.EducationException;
import com.ylm.commonutils.R;
import com.ylm.eduservice.entity.EduTeacher;
import com.ylm.eduservice.entity.vo.TeacherQuery;
import com.ylm.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.support.SimpleTriggerContext;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ylm
 * @since 2022-01-13
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
//@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    //@CrossOrigin
    @ApiOperation(value = "所有讲师列表")
    @GetMapping
    public R getAllTeachers(){
//        try{
//            int i=10/0;
//        }catch (Exception e){
//            throw new EducationException(20001,"自定义异常");
//        }
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("list",list);
    }

    //@CrossOrigin
    @ApiOperation(value = "删除讲师")
    @DeleteMapping("{id}")
    public R delTeacher(@PathVariable String id){
        boolean isRemove=eduTeacherService.removeById(id);
        if(isRemove)
            return R.ok();
        else
            return R.error();
    }

    //@CrossOrigin
    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("getTeacherPage/{current}/{limit}")
    public R getTeacherPage(@PathVariable Long current,
                            @PathVariable Long limit){
        Page<EduTeacher> page =new Page<>(current,limit);
        eduTeacherService.page(page,null);
        List<EduTeacher> records= page.getRecords();
        long total=page.getTotal();
        //1、存入Map返回
//        Map<String,Object> map=new HashMap<>();
//        map.put("list",records);
//        map.put("total",total);
//        return R.ok().data(map);
        //2、直接拼接
        return R.ok().data("list",records).data("total",total);
    }

    //@CrossOrigin
    @ApiOperation(value = "带条件分页查询讲师列表")
    @PostMapping("getTeacherPageVo/{current}/{limit}")
    public R getTeacherPageVo(@PathVariable Long current,
                              @PathVariable Long limit,
                              @RequestBody TeacherQuery teacherQuery){
        //@RequestBody 将json中查询体中的查询内容转为实体类
        //1、去除查询条件
        String name=teacherQuery.getName();
        Integer level=teacherQuery.getLevel();
        String begin=teacherQuery.getBegin();
        String end=teacherQuery.getEnd();
        //2、判断条件是否为空，不为空则在sql中加入条件判断
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }

        Page<EduTeacher> page=new Page<>(current,limit);
        eduTeacherService.page(page,wrapper);
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        //1、存入Map
//        Map<String,Object> map=new HashMap<>();
//        map.put("list",records);
//        map.put("total",total);
//        return R.ok().data(map);
        //2、直接拼接
        return R.ok().data("list",records).data("total",total);
    }

    //@CrossOrigin
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean isSave=eduTeacherService.save(eduTeacher);
        if(isSave)
            return R.ok();
        else
            return R.error();
    }

    //@CrossOrigin
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("getTeacherById/{id}")
    public R getTeacherById(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("eduTeacher",eduTeacher);
    }

    //@CrossOrigin
    @ApiOperation(value = "修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean isUpdate=eduTeacherService.updateById(eduTeacher);
        if(isUpdate)
            return R.ok();
        else
            return R.error();
    }

}

